<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="性别" clearable size="small">
          <el-option
            v-for="dict in genderOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="driverMobile">
        <el-input
          v-model="queryParams.driverMobile"
          placeholder="请输入司机手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="代驾经验" prop="experience">
        <el-select v-model="queryParams.experience" placeholder="代驾经验" clearable size="small">
          <el-option
            v-for="dict in experienceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="注册时间" prop="gmtTime">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

    </el-row>

    <el-table v-loading="loading" :data="driverList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="头像" align="center" prop="driverAvator">
        <template slot-scope="scope">
          <el-avatar class="demo-image__placeholder" shape="square" :size="80"  :src="ossPreview+ scope.row.driverAvator"  @click.stop="handleClickItem"></el-avatar>
        </template>

      </el-table-column>
      <el-table-column label="姓名" align="center" prop="driverName"/>
      <el-table-column label="性别" :formatter="genderFormat" align="center" prop="gender"/>
      <el-table-column label="手机号" align="center" prop="driverMobile"/>
      <el-table-column label="身份证号" align="center" prop="idCard"/>

      <el-table-column label="身份证正面" align="center" prop="idcardFront">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="ossPreview+ scope.row.idcardFront" >
          </el-image>
        </template>

      </el-table-column>
      <el-table-column label="身份证反面" align="center" prop="idcardBack">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="ossPreview+ scope.row.idcardBack">
          </el-image>
        </template>

      </el-table-column>
      <el-table-column label="驾驶证" align="center" prop="driverLicense">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="ossPreview+ scope.row.driverLicense">
          </el-image>
        </template>

      </el-table-column>
      <el-table-column label="驾驶证" align="center" prop="driverLicenseBack">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="ossPreview+ scope.row.driverLicenseBack">
          </el-image>
        </template>

      </el-table-column>

      <el-table-column label="省" align="center" prop="province"/>
      <el-table-column label="市" align="center" prop="city"/>
      <el-table-column label="县" align="center" prop="town"/>
      <el-table-column label="代驾经验" :formatter="experienceFormat" align="center" prop="experience"/>
      <el-table-column label="注册时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['driver:driver:audit']"
          >审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 入驻审核对话框 -->
    <el-dialog title="入驻审核" :visible.sync="open" v-if="open" :destroy-on-close="destroy" width="70%">

      <apply-audit :driverId="driverId"  @handleDriverAudit="handleDriverAudit"></apply-audit>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listDriverApply,   addDriver, updateDriver} from "@/api/driver/driver";
  import ApplyAudit from "./apply-audit";
  import defaultSettings from '@/settings'
  export default {
    name: "Driver",
    components: {ApplyAudit},
    data() {
      return {
        driverId: null,
        ossPreview: defaultSettings.ossPreview,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 代驾司机表格数据
        driverList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        destroy: true,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          gender: undefined,
          driverName: undefined,
          driverMobile: undefined,
          idCard: undefined,
          isEnable: undefined,
          orderSwitch: undefined,
          password: undefined,
          agentFee: undefined,
          experience: undefined,
          isWxsub: undefined,
          gmtTime: undefined
        },
        // 性别数据字典
        genderOptions: [],
        // 状态数据字典
        enableOptions: [],
        driverStateOptions: [],
        // 驾驶经验数据字典
        experienceOptions: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        // 日期范围
        dateRange: [],
        dId:undefined,
        isWxSubOptions: [],
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_user_sex").then(response => {
        this.genderOptions = response.data;
      });
      this.getDicts("driver_state").then(response => {
        this.driverStateOptions = response.data;
      });
      this.getDicts("sys_is_enable").then(response => {
        this.enableOptions = response.data;
      });
      this.getDicts("driving_experience").then(response => {
        this.experienceOptions = response.data;
      });
      this.getDicts("is_wxsub").then(response => {
        this.isWxSubOptions = response.data;
      });
    },
    methods: {
      /** 修改按钮操作 */
      handleResetPwd(row) {
        this.dId = row.id
        this.$refs.resetPwd.open();
      },
      /** 查询代驾司机列表 */
      getList() {
        this.loading = true;
        listDriverApply(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.driverList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      emitChooseStation() {
        this.stationOpen = false;
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          gender: undefined,
          driverName: undefined,
          driverAvator: undefined,
          driverMobile: undefined,
          idcardFront: undefined,
          idcardBack: undefined,
          idCard: undefined,
          isEnable: undefined,
          orderSwitch: undefined,
          openId: undefined,
          password: undefined,
          agentFee: undefined,
          experience: undefined,
          gmtTime: undefined,
          locPoi: "郑州市人民政府",
          coordinate: "113.625351,34.746303"
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.driverId = row.id
        this.open = true;
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.coordinate = this.postForm.longitude + "," + this.postForm.latitude;
            this.form.locPoi = this.postForm.detailedAddress;
            if (this.form.id != undefined) {
              updateDriver(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addDriver(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      getPosition({ address, lat, lng, name }) {
        this.postForm.detailedAddress = address;
        this.postForm.longitude = String(lng);
        this.postForm.latitude = String(lat);
      },
      handleDriverAudit(data){
        this.open = false;
        this.getList();
      },
      genderFormat(row, column) {
        return this.selectDictLabel(this.genderOptions, row.gender);
      },
      enableFormat(row, column) {
        return this.selectDictLabel(this.driverStateOptions, row.isEnable);
      } ,
      switchFormat(row, column) {
        return this.selectDictLabel(this.enableOptions, row.orderSwitch);
      } ,
      experienceFormat(row, column) {
        return this.selectDictLabel(this.experienceOptions, row.experience);
      },
      isWxsubFormat(row, column) {
        return this.selectDictLabel(this.isWxSubOptions, row.isWxsub);
      },
      handleClickItem(){
        // 获取遮罩层dom

        this.$nextTick(() => {
          console.log("111111111111111111111111")
          let domImageMask = document.querySelector(".el-image-viewer__mask");
          if (!domImageMask) {
            console.log("2222222222222222222222")
            return;
          }
          domImageMask.addEventListener("click", () => {
            // 点击遮罩层时调用关闭按钮的 click 事件
            document.querySelector(".el-image-viewer__close").click();
          });
        })

      }
    }
  };
</script>
